package com.css.fxfzxczh.modules.seismicIntensityMap.seismicIntensityDraw.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.fxfzxczh.base.remote.RemoteService;
import com.css.fxfzxczh.base.response.RestResponse;
import com.css.fxfzxczh.modules.seismicIntensityMap.seismicIntensityDraw.param.QueryParam;
import com.css.fxfzxczh.modules.seismicIntensityMap.seismicIntensityDraw.service.SeismicIntensityDrawService;
import com.css.fxfzxczh.modules.seismicIntensityMap.seismicIntensityDraw.vo.ResultVO;
import com.css.fxfzxczh.modules.seismicIntensityMap.seismicIntensityDraw.vo.VersionPageVO;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import com.css.fxfzxczh.util.PlatformPageUtils;
import com.css.fxfzxczh.util.ResponseCreator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author Dbj
 * @date 2023年06月21日  15:12
 * 地震烈度绘制
 */
@RestController
@RequestMapping("/seismicIntensityDraw")
public class SeismicIntensityDrawController {

    @Value("${jcsj.url}")
    private String jcsjUrl;
    @Resource
    SeismicIntensityDrawService seismicIntensityDrawService;
    @Resource
    RemoteService remoteService;

    /**
     * 根据勾选项获取数据渲染图层
     */
    @GetMapping("/getSelectedData")
    public RestResponse getSelectedData( QueryParam queryParam){
        String selectedItem = queryParam.getSelectedItem();
        if(PlatformObjectUtils.isEmpty(selectedItem)){
            return ResponseCreator.createFailResponse("请选择要展示的数据项");
        }
        if(Arrays.asList(selectedItem.split(",")).contains("4")){
            if(StringUtils.isBlank(queryParam.getFaultVersion())){
                return ResponseCreator.createFailResponse("请选择断层数据版本");
            }
        }
        if(Arrays.asList(selectedItem.split(",")).contains("6")){
            if(StringUtils.isBlank(queryParam.getHouseVersion())){
                return ResponseCreator.createFailResponse("请选择房屋数据版本");
            }
        }
        if(Arrays.asList(selectedItem.split(",")).contains("7")){
            if(StringUtils.isBlank(queryParam.getDemVersion())){
                return ResponseCreator.createFailResponse("请选择DEM数据版本");
            }
        }
        ResultVO resultVO = seismicIntensityDrawService.getSelectedData(queryParam);
        return ResponseCreator.createSuccessResponse(resultVO);
    }

    /**
     * 查询区划13张表的版本信息
     */
    @GetMapping("/get13TableVersionList")
    public RestResponse get13TableVersionList(@RequestParam Integer rows,@RequestParam Integer page ){
        if(rows==null){
            rows = 10;
        }
        if(page==null){
            page = 1;
        }
        return ResponseCreator.createSuccessResponse(seismicIntensityDrawService.get13TableVersionList(rows,page));
    }


    /***
     * 基础数据系统版本查询
     */
    @GetMapping("/getJcsjVersionPage")
    public RestResponse getJcsjVersionPage(HttpServletRequest request){
        RestResponse restResponse = null;
        try {
            int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.parseInt(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            String dataType2 = request.getParameter("dataType2");
            //dataType2  93 断层 68 DEM高程
            String url = jcsjUrl + "/datacenter/sysDataVersion/page?dataType2="+dataType2;
            url += "&page="+curPage+"&rows="+pageSize+"&hasNationwide=1";
            JSONObject valueMap = new JSONObject();
            restResponse = remoteService.getJCSJ(url, 2, valueMap);
            restResponse.setMessage("获取成功！");
        } catch (Exception e) {
            e.printStackTrace();
            restResponse = RestResponse.fail("获取失败！");
        }
        return restResponse;
    }

    @PostMapping("/getParam1Version")
    public RestResponse getParam1Version(@RequestBody Map<String, Object> map) {
        RestResponse restResponse = null;
        try {
            VersionPageVO vo= JSON.parseObject(JSON.toJSONString(map.get("vo")),VersionPageVO.class);
            restResponse = seismicIntensityDrawService.getParam1Version((String) map.get("tab"), vo);
            restResponse.setMessage("获取成功！");
        } catch (Exception e) {
            e.printStackTrace();
            restResponse = RestResponse.fail("获取失败！");
        }
        return restResponse;
    }
}
